package com.njxzc.igo.Dao;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections.list.TreeList;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.njxzc.igo.model.Products;
import com.njxzc.igo.model.ShoppingCart;
import com.njxzc.igo.model.User;
import com.njxzc.igo.utils.HibernateUtils;

import sun.nio.ch.SelChImpl;

public class ShoppingCartDaoImpl implements ShoppingCartDao {

	@Override
	public void saveShoppingCart(User user, long productId, int amount) {
		Session session = null;
		Transaction tx = null;
		ShoppingCart sc = new ShoppingCart();
		try {
			session = HibernateUtils.getSession();
			tx = session.beginTransaction();

			sc.setProductId(productId);
			sc.setCreateTime(new Date());
			sc.setUser(user);
			sc.setAmount(amount);

			session.save(sc);
			tx.commit();
		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
			}
		}
	}

	@Override
	public List<ShoppingCart> findCartByUsernameAndProdId(String name, long productId) {
		Session session = null;
		Transaction tx = null;
		List<ShoppingCart> list = null;
		try {
			session = HibernateUtils.getSession();
			tx = session.beginTransaction();
			list = session.createQuery("from ShoppingCart as sc where sc.productId=? and sc.user.username=?")
					.setLong(0, productId).setString(1, name).list();
			tx.commit();
		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
			}
		}
		return list;
	}

	@Override
	public void updateShoppingCart(ShoppingCart cart, int amounts) {
		Session session = null;
		Transaction tx = null;
		int amount = cart.getAmount();
		try {
			session = HibernateUtils.getSession();
			tx = session.beginTransaction();
			session.update(cart);
			cart.setAmount(amount + amounts);
			session.save(cart);
			tx.commit();
		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
			}
		}
	}

	@Override
	public List<ShoppingCart> loadMyShoppingCart(Set<ShoppingCart> shoppingset) {

		List<ShoppingCart> list = new ArrayList<ShoppingCart>(shoppingset);
		Session session = null;
		Transaction tx = null;
		int i = 0;
		try {
			session = HibernateUtils.getSession();
			tx = session.beginTransaction();
			for (ShoppingCart sc : shoppingset) {
				i++;
				long id = sc.getProductId();
				Products products = (Products) session.get(Products.class, id);
				sc.setProductExample(products);
				if (i % 10 == 0) {
					session.flush();
					session.clear();
				}
			}

			tx.commit();
		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
			}
		}
		return list;
	}

	
	public List<ShoppingCart> loadMyShoppingCart(List<ShoppingCart> shoppingset) {

		Session session = null;
		Transaction tx = null;
		int i = 0;
		try {
			session = HibernateUtils.getSession();
			tx = session.beginTransaction();
			for (ShoppingCart sc : shoppingset) {
				i++;
				long id = sc.getProductId();
				Products products = (Products) session.load(Products.class, id);
				sc.setProductExample(products);
				if (i % 10 == 0) {
					session.flush();
					session.clear();
				}
			}

			tx.commit();
		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
			}
		}
		return shoppingset;
	}
	@Override
	public void updateShoppingCart(long	 productId, long userId, int amount) {
		Session session = null;
		Transaction tx = null;
		try {
			session = HibernateUtils.getSession();
			tx = session.beginTransaction();
			List list = session.createQuery("from ShoppingCart as sc where sc.productId=? and sc.user.userId=?")
					.setLong(0, productId).setLong(1, userId).list();
			ShoppingCart sc = (ShoppingCart) list.get(0);
			sc.setAmount(amount);
			tx.commit();
		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
			}
		}

	}

	@Override
	public void saveOrUpdateAll(Map map) {
		Session session = null;
		Transaction tx = null;
		Iterator it = map.entrySet().iterator();
		try {
			session = HibernateUtils.getSession();
			tx = session.beginTransaction();
			while (it.hasNext()) {
				Map.Entry entry = (Map.Entry) it.next();
				List<ShoppingCart> temp = (List) entry.getValue();
				for (ShoppingCart sc : temp) {
					session.saveOrUpdate(sc);
				}
			}
			tx.commit();
		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
			}
		}

	}

	@Override
	public void deleteShoppingcartById(String shopId) {
		Session session = null;
		Transaction tx = null;
		try {
			session = HibernateUtils.getSession();
			tx = session.beginTransaction();
			ShoppingCart sc = (ShoppingCart) session.get(ShoppingCart.class, shopId);
			session.delete(sc);
			tx.commit();
		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
			}
		}finally{
			HibernateUtils.closeSession();
		}
	}

	@Override
	public void deleteByBatch(List shoppingCartIds) {
		Session session = null;
		Transaction tx = null;
		String hql = "delete from ShoppingCart where shoppingCartId in (:ids)";
		try {
			session = HibernateUtils.getSession();
			tx = session.beginTransaction();
			session.createQuery(hql).setParameterList("ids",shoppingCartIds).executeUpdate();
			tx.commit();
		} catch (Exception e) {
			if(tx!=null){
				tx.rollback();
			}
			e.printStackTrace(System.out);
		}finally {
			HibernateUtils.closeSession();
		}
	}
	
	@Override
	public void deleteFromIds(String[] shoppingCartIds) {
		Session session = null;
		Transaction tx = null;
		String hql = "delete from ShoppingCart where shoppingCartId in (:ids)";
		try {
			session = HibernateUtils.getSession();
			tx = session.beginTransaction();
			session.createQuery(hql).setParameterList("ids", shoppingCartIds).executeUpdate();
			tx.commit();
		} catch (Exception e) {
			if(tx!=null){
				tx.rollback();
			}
		}finally {
			HibernateUtils.closeSession();
		}
		
	}
	@Override
	public List<ShoppingCart> findAllInIds(List<String>ShoppingIds) {
		Session session = null;
		Transaction tx = null;
		List<ShoppingCart> selected = null;
		try {
			session = HibernateUtils.getSession();
			tx = session.beginTransaction();
			
			selected=  session.createCriteria(ShoppingCart.class).add(Restrictions.in("shoppingCartId",ShoppingIds)).list();
			tx.commit();
		} catch (Exception e) {
			if(tx!=null){
				tx.rollback();
			}
			e.printStackTrace(System.out);
		}finally {
			//HibernateUtils.closeSession();
		}
		return selected;
	}
	public static void main(String[] args) {
		ShoppingCartDao scd = new ShoppingCartDaoImpl();
		
		List list = scd.findAllInIds(new ArrayList<>());
		System.out.println(list);
		
	}

	@Override
	public List<ShoppingCart> loadMyShoppingCart(long userId) {
		Session session = null;
		Transaction tx = null;
		List list = null;
		try {
			session = HibernateUtils.getSession();
			tx = session.beginTransaction();
			list =session.createQuery("from ShoppingCart as sc where sc.user.userId=?").setLong(0, userId).list();
			tx.commit();
		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
			}
		}
		return list;
	}


}
